package com.moniteach.server.servlet;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.moniteach.server.db.bo.Lesson;
import com.moniteach.server.db.bo.LessonTheme;
import com.moniteach.server.db.bo.Theme;
import com.moniteach.server.db.connector.PMF;
import com.moniteach.utils.Resource;

public class LessonThemeServlet extends CommonServlet {
	private String action;
	private String id;
	private String lesson;
	private String theme;

	@Override
	protected void prepare(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.setCharacterEncoding("UTF-8");

		action = req.getParameter(Resource.Const.action);
		if ("".equals(action) || action == null) {
			resp.getWriter().print(Resource.Const.no + "ACTION");
			return;
		}

		id = req.getParameter(Resource.Const.id);
		lesson = req.getParameter(Resource.Const.lesson);
		theme = req.getParameter(Resource.Const.theme);

		String response = "";
		if (Resource.Action.CREATE.equals(action)) {
			response = create();
		} else if (Resource.Action.GET_BY_ID.equals(action)) {
			response = getById();
		} else if (Resource.Action.MODIFY.equals(action)) {
			response = modify();
		}
		resp.getWriter().print(response);

	}

	@Override
	protected String create() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Long lessonLong = Long.valueOf(lesson);
			Lesson entityLesson = pm.getObjectById(Lesson.class, lessonLong);

			Long themeLong = Long.valueOf(theme);
			Theme entityTheme = pm.getObjectById(Theme.class, themeLong);
			LessonTheme oneLessonTheme = new LessonTheme(entityLesson.getId(),
					entityTheme.getId());
			pm.makePersistent(oneLessonTheme);
			return String.valueOf(oneLessonTheme.getId());
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}
	}

	@Override
	protected String get() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getById() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Long idLong = Long.valueOf(id);
			LessonTheme entityLessonTheme = pm.getObjectById(LessonTheme.class,
					idLong);
			return entityLessonTheme.toJSONObject().toString();
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}
	}

	@Override
	protected String getAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String modify() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Long idLong = Long.valueOf(id);
			LessonTheme entityLessonTheme = pm.getObjectById(LessonTheme.class,
					idLong);

			if (lesson != null) {
				Long lessonLong = Long.valueOf(lesson);
				Lesson entityLesson = pm
						.getObjectById(Lesson.class, lessonLong);
				entityLessonTheme.setLesson(entityLesson.getId());
			}
			if (theme != null) {
				Long themeLong = Long.valueOf(theme);
				Theme entityTheme = pm.getObjectById(Theme.class, themeLong);
				entityLessonTheme.setTheme(entityTheme.getId());
			}
			pm.makePersistent(entityLessonTheme);
			return Resource.Const.ok;
		} catch (Exception e) {
			return Resource.Const.no;
		} finally {
			pm.close();
		}
	}
}
